import Vue from 'vue';
import { Loading, Message } from 'element-ui';
let loadingInstance = null;
//请求成功
const baseUrl = 'http://localhost:3004/'

function handleSuccess(res) {
    if (res) {
        //跳转到登录页由 前端 做处理
        loadingInstance && loadingInstance.close();
        let code = res.data.code;
        // 	let url = "http://test.evcard.vip:8088/evcard-sso";
        if (code == "00000004") {
            let url = res.data.data.redirectUrl;
            if (metadata.host && metadata.host == '/' || !res.data) {
                url = `http://csms-${metadata.build_env}.evcard.vip/evcard-sso/login.html` + '?redirectUrl=' + window.location.href;
            } else {
                url = url + '?redirectUrl=' + window.location.href;
            }
            window.location.href = url;

        } else {
            return res.data
        }
    }
}
//请求失败
function handleError(err) {
    loadingInstance && loadingInstance.close();
    Message.error({ message: '网络错误，请稍后重试！', duration: 1000 });
    console.log(err)
}

// get请求
function getAjax(type, url, params) {
    return Vue.http[type](
        baseUrl + url, { params }
    ).then(res => handleSuccess(res), err => handleError(err))
}

// post请求
function postAjax(type, url, body, params) {
    return Vue.http[type](
        baseUrl + url,
        body, { params }
    ).then(res => handleSuccess(res), err => handleError(err))
}
//基础请求方法
function baseAjax(type, url, body, params) {
    switch (type) {
        case 'get':
        case 'delete':
            return getAjax(type, url, params)
        case 'post':
        case 'put':
            return postAjax(type, url, body, params)
    }

}

// 输出get请求
function hget(url, params) {
    return baseAjax('get', url, {}, params)
        .then(res => res)
}
// 输出loading框get
function hgetl(url, params) {
    loadingInstance = Loading.service();
    return baseAjax('get', url, {}, params)
        .then(res => res)
}
// 特殊的get请求，
function hgets(url, params) {
    let query = '';
    for (let key in params) {
        query += `/${key}/${params[key]}`;
    }
    return Vue.http.get(
        url + query, {}
    ).then(res => handleSuccess(res), err => handleError(err))
}

function hgetsl(url, params) {
    let query = '';
    for (let key in params) {
        query += `/${key}/${params[key]}`;
    }
    loadingInstance = Loading.service();
    return Vue.http.get(
        url + query, {}
    ).then(res => handleSuccess(res), err => handleError(err))
}
// 输出post请求
function hpost(url, body, params) {
    // if(!params) params = body;
    return baseAjax('post', url, body, params)
}

function hpostl(url, body, params) {
    loadingInstance = Loading.service();
    // if(!params) params = body;
    return baseAjax('post', url, body, params)
}
//输出put请求
function hput(url, body, params) {
    return baseAjax('put', url, body, params)
}

function hputs(url, body, params) {
    let query = '';
    for (let key in params) {
        query += `/${key}/${params[key]}`;
    }
    return Vue.http.put(url + query, body).then(res => handleSuccess(res), err => handleError(err))
}

function hputl(url, body, params) {
    loadingInstance = Loading.service();
    return baseAjax('put', url, body, params)
}

export default {
    get: hget,
    getl: hgetl,
    post: hpost,
    postl: hpostl,
    put: hput,
    puts: hputs,
    putl: hputl,
    gets: hgets,
    getsl: hgetsl
}